/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.gu.derp.model;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 *
 * @author jakob
 */
public class Feed {
    private final String name;
    private final URL url;
    private final List<Item> items = new ArrayList<Item>();

    public Feed(String name, URL url) {
        this.name = name;
        this.url = url;
    }

    public String getName() {
        return name;
    }

    public URL getUrl() {
        return url;
    }
    
    public List<Item> getItems() {
        // Create new list to make sure no one tampers with the original.
        return new ArrayList<Item>(items);
    }
    
    public void addItem(Item item) {
        items.add(item);
    }
    
    // Use DOM interface to convert feed XML into a Feed object.
    // Possible alternatives: XSLT or JAXB serialization.
    public static Feed fromURL(URL url) {
        Feed feed = null;
        
        try {
            HttpURLConnection request = (HttpURLConnection) url.openConnection();
            try {
                InputStream response = request.getInputStream();
                Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(response);
                Element channel = (Element) xml.getDocumentElement().getElementsByTagName("channel").item(0);
                feed = new Feed(channel.getElementsByTagName("title").item(0).getTextContent(), url);
                
                // All entries in an item are optional as defined by the RSS spec.
                NodeList items = channel.getElementsByTagName("item");
                for (int i = 0; i < items.getLength(); i++) {
                    Element item = (Element) items.item(i);
                    
                    String title;
                    if (item.getElementsByTagName("title").item(0) == null) {
                        title = "[Untitled]";
                    } else {
                        title = item.getElementsByTagName("title").item(0).getTextContent();
                    }
                    
                    SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
                    Date date = format.parse(item.getElementsByTagName("pubDate").item(0).getTextContent());
                    
                    String description = null;
                    if (item.getElementsByTagName("description").item(0) != null) {
                        description = item.getElementsByTagName("description").item(0).getTextContent();
                    }
                    
                    String link = null;
                    if (item.getElementsByTagName("link").item(0) != null) {
                        link = item.getElementsByTagName("link").item(0).getTextContent();
                    }
                    
                    feed.addItem(new Item(title, date, description, link));
                }
            } finally {
                request.disconnect();
            }
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
        
        return feed;
    }
}
